package com.dao;

import com.pojo.Provider;
import com.pojo.User;
import com.utils.DSUtil;
import com.utils.JDBCUtils;

import java.sql.Date;
import java.util.List;

public class UserDao {
    //<th width="10%">用户编码</th>
    //			<th width="20%">用户名称</th>
    //			<th width="10%">性别</th>
    //			<th width="10%">年龄</th>
    //			<th width="10%">电话</th>
    //			<th width="10%">用户角色</th>
    //			<th width="30%">操作</th>

    public List<User> queryUserAll(){
        List users = JDBCUtils.selectAll("select id, userCode,userName,gender,birthday,phone,userRoleName from user");
        return users;
    }


    public boolean AddUser(int userCode , String userName,String userpassword, String ruserpassword, boolean gender, Date birthday, String phone, String address, String userRoleName){
        int update = JDBCUtils.update("insert into user (userCode,userName,userPassword,ruserPassword,gender,birthday,phone,address,userRoleName) values (?,?,?,?,?,?,?,?,?)", userCode, userName, userpassword, ruserpassword, gender, birthday, phone, address, userRoleName);
        return update>0;
    }

    public List QueryById(String id){
        List<User> selectone = JDBCUtils.selectone(User.class, "select * from user where id = ?", id);
        return selectone;
    }

    public boolean DeleteById(String id){
        int update = JDBCUtils.update("delete from user where id = ?", id);
        return update>0;
    }

    public boolean updateuser(String id,String userName,boolean gender,Date birthday,String phone,String userRoleName){
        int update = JDBCUtils.update("update user set userName=?,gender=?, birthday=?,phone=?, userRoleName=? where id = ?",userName,gender,birthday,phone,userRoleName,id);
        return update>0;
    }

    public List<User> queryByName(String ...args){
        args[0] = "%"+args[0]+"%";
        args[1] = "%"+args[1]+"%";
        return JDBCUtils.selectone(User.class, "select * from user where userName like ? and userRoleName like ?", args[0], args[1]);
    }

    public User queryOne(String username) {
        return DSUtil.query("select * from user where username = ?",User.class,username).get(0);
    }

    public void updatePwd(String username, String newpassword) {
        DSUtil.execute("update user set userPassword = ? where username = ?",newpassword,username);
    }

    public static void main(String[] args) {
        UserDao ud = new UserDao();
        for (User user : ud.selectByPage(0, 2)) {
            System.out.println(user);
        }
    }

    public List<User> queryByUserName(String userName) {
        return DSUtil.query("select * from user where userName = ?",User.class,userName);
    }

    public List<User> selectByPage(int pagenum,int pagesize){
        return JDBCUtils.selectOne("select * from user limit ?,?", pagenum, pagesize);
    }

    public List<User> queryLimit(Integer currentPageNo, Integer pageSize) {
        return DSUtil.query("select * from user limit ?,?", User.class, (currentPageNo - 1) * pageSize, pageSize);
    }
}
